/***********************************************************************
** Copyright (C) 2011 Anthony Naddeo <anthony.naddeo@gmail.com>
**
** This file is part of LinkedUp
**
** LinkedUp is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** LinkedUp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this package; If not, see <http://www.gnu.org/licenses/>.
**
***********************************************************************/
import QtQuick 1.1
import com.meego 1.0
import com.nokia.extras 1.0
import "../components"

MyPage {
	id: root
	anchors.fill: parent
	title: "Profile"

	property string profileID

	tools: ToolBarLayout {
		ToolIcon { platformIconId: "toolbar-view-menu";
			anchors.right: parent===undefined ? undefined : parent.right
			onClicked: (myMenu.status == DialogStatus.Closed) ? myMenu.open() : myMenu.close()
		}
		ToolIcon { platformIconId: "toolbar-back";
			anchors.left: parent===undefined ? undefined : parent.left
			visible: pageStack ? pageStack.depth > 1 ? true : false : false
			onClicked: {
				if(pageStack.depth > 1)
					pageStack.pop()
			}
		}
		ButtonRow{
			style: TabButtonStyle{}
			ToolIcon { platformIconId: "toolbar-send-email";
				onClicked: messageSheet.status == DialogStatus.Open ? messageSheet.close() : messageSheet.open()
			}
		}

	}


	onPostFinished: {
		var obj = banner.createObject(root)
		obj.text = (statusCode == "201" ? "Message sent" : "Server rejected message")
		obj.show()
		obj.destroy(5000)
	}



	Component.onCompleted: get(API.get_person_by_id(profileID))
	onGetFinished: {//get a public profile instead of private one if not available
			if(statusCode === "401" || statusCode === "403"){
				get(API.get_person_public_by_id(profileID))
			}else{
				model.xml = responseText
			}
		}

	Component{
		id: banner
		InfoBanner{timerEnabled: true; timerShowTime: 3000; z:100}
	}

	LinkedInMessageSheet{
		id: messageSheet; z:100
		recipient: profileID
		onSend: post(API.post_message(xml))
		visualParent: root
	}

	ProfileModel{id: model
		onStatusChanged: {
			if(status == XmlListModel.Ready && get(0))
				root.title = get(0).firstName + " " + get(0).lastName
		}
	}

	ListView{
		id: general
		anchors.fill: parent
		anchors.topMargin: borderHeight+10
		anchors.leftMargin: 10
		anchors.rightMargin: 10

		model: model
		delegate: ProfileDelegate{}
	}



}
